using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using LogFileViewer.Properties;
using System.Collections.Specialized;
using System.ComponentModel;

namespace LogFileViewer
{
    public class DataAccess
    {

        public enum FileHandling
        {
            SingleFileHandling = 0,
            MultiFileHandling = 1
        }

        public enum DataHandling
        {
            DataBuffering = 0,
            LineIndexing = 1
        }

        public enum ReaderHandling
        {
            ReadFull = 0,
            ActiveTypes = 1,
            ActiveRows = 2
        }

        private FileHandling fileHandle;

        /// <summary>
        ///     <u>Performance Setting: </u>
        ///     Specifies the variant in handling log file.
        /// </summary>
        public FileHandling FileHandle
        {
            get { return fileHandle; }
            set { fileHandle = value; }
        }

        private DataHandling dataHandle;

        /// <summary>
        ///     <u>Performance Setting: </u>
        ///     Specifies the variant in managing data when
        ///     reading into memory.
        /// </summary>
        public DataHandling DataHandle
        {
            get { return dataHandle; }
            set { dataHandle = value; }
        }

        private ReaderHandling readerHandle;

        /// <summary>
        ///     <u>Performance Setting: </u>
        ///     Specifies the variant in reading data when
        ///     necessary for GUI.
        /// </summary>
        public ReaderHandling ReaderHandle
        {
            get { return readerHandle; }
            set { readerHandle = value; }
        }

        /// <summary>
        ///     This is used when "Read Active Types" or "Read Active Rows"
        ///     options are enabled. It represents all currently active
        ///     message types selected in GUI.
        /// </summary>
        public MessageTypes[] SelectedTypes
        {
            get 
            {
                StringCollection strgCol = Settings.Default.SelectedTypes;
                if (strgCol != null && strgCol.Count > 0)
                {
                    MessageTypes[] outputArray = new MessageTypes[strgCol.Count];
                    int i = 0;
                    foreach (string activatedType in strgCol)
                    {
                        outputArray[i] = MessageType.GetMessageType(activatedType);
                        i++;
                    }
                    return outputArray;
                }
                return null;
            }
        }

        /// <summary>
        ///     This is used when "Read Active Types" or "Read Active Rows"
        ///     options are enabled. It represents all currently active
        ///     message types selected in GUI.
        /// </summary>
        public string[] SelectedTypesAsString
        {
            get
            {
                StringCollection strgCol = Settings.Default.SelectedTypes;
                if (strgCol != null && strgCol.Count > 0)
                {
                    string[] outputArray = new string[strgCol.Count];
                    int i = 0;
                    foreach (string activatedType in strgCol)
                    {
                        outputArray[i] = activatedType;
                        i++;
                    }
                    return outputArray;
                }
                return null;
            }
        }

        private Dictionary<MessageTypes, int> everyLogEntriesAmount;

        /// <summary>
        ///     Contains the amount of log messages (length of list)
        ///     for each MessageType (of all files).
        /// </summary>
        public Dictionary<MessageTypes, int> EveryLogEntriesAmount
        {
            get { return everyLogEntriesAmount; }
            set { everyLogEntriesAmount = value; }
        }

        public void setEveryLogEntriesAmount(MessageTypes type, int amount)
        {
            if (this.everyLogEntriesAmount == null)
            {
                this.everyLogEntriesAmount = new Dictionary<MessageTypes, int>(MessageType.Length());
            }
            if (!this.everyLogEntriesAmount.ContainsKey(type))
            {
                this.everyLogEntriesAmount.Add(type, amount);
            }
            else
            {
                this.everyLogEntriesAmount[type] = amount;
            }
        }

        private int everyLogEntriesAmountAll = 0;

        /// <summary>
        ///     Returns the sum of all activated log type list lengths (of all files).
        /// </summary>
        public int EveryLogEntriesAmountAll 
        {
            get
            {
                if (everyLogEntriesAmount != null)
                {
                    int sum = 0;
                    for (int i = 0; i < this.SelectedTypes.Length; i++)
                    {
                        MessageTypes currentType = this.SelectedTypes[i];
                        if (this.everyLogEntriesAmount.ContainsKey(currentType))
                        {
                            sum += this.everyLogEntriesAmount[currentType];
                        }
                    }
                    return sum;
                }
                return everyLogEntriesAmountAll;
            }
            set
            {
                this.everyLogEntriesAmountAll = value;
            }
        }

        private LogFileProvider fnProvider;

        /// <summary>
        ///     This object holds information about files used
        ///     in currently opened log file.
        /// </summary>
        public LogFileProvider FnProvider
        {
            get { return fnProvider; }
            set { fnProvider = value; }
        }

        private Dictionary<MessageTypes, long[]> lineIndexes;

        /// <summary>
        ///     When line indexing is activated, this Dictionary
        ///     holds all indexes of all log files, accessable by MessageType.
        /// </summary>
        public Dictionary<MessageTypes, long[]> LineIndexes
        {
            get { return lineIndexes; }
            set { lineIndexes = value; }
        }

        private LogFile currentLogFile;

        /// <summary>
        ///     Represents the currently opened logfile. Is null if nothing opened.
        /// </summary>
        public LogFile CurrentLogFile
        {
            get { return currentLogFile; }
            set { currentLogFile = value; }
        }
    }
}
